<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8" />
	<title>维修人员管理</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />

	<!-- App favicon -->
	<link rel="shortcut icon" href="assets/images/favicon.ico">

	<!-- App css -->
	<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/icons.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/metismenu.min.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" type="text/css" href="assets/css/custom.css" />
	<!-- layer插件 -->
	<link rel="stylesheet" type="text/css" href="plugins/layer2.4/skin/layer.css" />

	<!-- bootstrap-select -->
	<link rel="stylesheet" type="text/css" href="plugins/bootstrap-select/css/bootstrap-select.min.css" />
	<!-- jq三联级插件 -->
	<link href="assets/css/tntreebox.css?v=27" rel="stylesheet" type="text/css">
	<script src="assets/js/modernizr.min.js"></script>

	<style>
		.selectBox {
			/* 控制选中框与文字的距离 */
			margin-right: 8px;
		}

		#deleteAll {
			/* 控制清空按钮与复选框的距离 */
			margin-bottom: 12px;
		}
	</style>

</head>

<body>

	<!-- Begin page -->
	<div id="wrapper">

		<!-- ============================================================== -->
		<!-- 页面内容开始 -->
		<!-- ============================================================== -->

		<!-- Start Page content -->
		<div class="my-content">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="card-box">
							<h4 class="m-t-0 m-b-30 header-title">分配维修人员</h4>

							<form class="form-horizontal" role="form">
								<div class="form-group row">
									<input type="text" class="form-control" id="id" name="id" hidden>
									<label class="col-3 col-form-label">屠宰市场名称</label>
									<div class="col-9">
										<input type="text" class="form-control" id="housename" disabled>
									</div>
								</div>
								<div class="form-group row">
									<label class="col-3 col-form-label">维修人员列表</label>
									<div class="col-9" id="checkBoxes"
										style="padding: 20px 0 20px 10px; border: 0.5px solid #dee2e6; margin: 0 auto;">
										<button type="button" id="deleteAll" class="btn btn-primary waves-effect" >清空所有维修人员</button>
										<br>
									</div>
								</div>
								<div class="form-group mb-0 justify-content-end row">
									<div class="col-9">
										<button type="submit" class="btn btn-primary waves-effect">提交</button>
										<button type="button" onclick="layer_close()"
											class="btn btn-danger waves-effect">关闭</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>

			</div>
			<!-- container -->

		</div>

		<!-- ============================================================== -->
		<!-- End Right content here -->
		<!-- ============================================================== -->

	</div>
	<!-- END wrapper -->

	<!-- jQuery  -->
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/js/bootstrap.bundle.min.js"></script>
	<script src="assets/js/metisMenu.min.js"></script>
	<script src="assets/js/waves.js"></script>
	<script src="assets/js/jquery.slimscroll.js"></script>
	<script src="assets/js/tntreebox.js?v=35"></script>


	<!-- App js -->
	<script src="assets/js/custom.js" type="text/javascript" charset="utf-8"></script>
	<script src="assets/js/jquery.core.js"></script>
	<script src="assets/js/jquery.app.js"></script>
	<script src="assets/js/custom.js" type="text/javascript" charset="utf-8"></script>

	<!-- 表单验证 -->
	<script src="plugins/jquery.form.js/jquery.form.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="plugins/jquery.validation.1.14.0/jquery.validate.js" type="text/javascript" charset="utf-8"></script>
	<script src="plugins/jquery.validation.1.14.0/validate-methods.js" type="text/javascript" charset="utf-8"></script>
	<script src="plugins/jquery.validation.1.14.0/messages_zh.js" type="text/javascript" charset="utf-8"></script>
	<!-- bootstrap-select -->
	<script src="plugins/bootstrap-select/js/bootstrap-select.js" type="text/javascript" charset="utf-8"></script>

	<!-- layer插件 -->
	<script src="plugins/layer2.4/layer.js" type="text/javascript" charset="utf-8"></script>

	<!-- easyUI -->
	<script src="plugins/jquery-easyUI/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="plugins/jquery-easyUI/easyui-lang-zh_CN.js" type="text/javascript" charset="utf-8"></script>

	<!-- datatables插件 -->
	<script type="text/javascript">
		$(function () {
			// 点击一键取消复选框的选中
			$("#deleteAll").click(function(){
				$("#checkBoxes input[type='checkbox']").each(function() {
				this.checked = false})
			})

			// 获取到屠宰场ID
			code = get_code()

			//根据屠宰场ID查询到屠宰场名称，再渲染到页面
			$.ajax({
				type: "get",
				url: URL + 'slaughterHouse/id?id=' + code,
				async: false,
				dataType: "json",
				success: function (data) {

					data = data.object

					// 展示屠宰场名称
					$("#housename").attr("placeholder", data.name);

				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					networkError(XMLHttpRequest, textStatus, errorThrown)
				}
			});

			// 获取所有维修人员列表
			$.ajax({
				type: "get",
				url: URL + 'queryRepairs',
				async: false,
				dataType: "json",
				success: function (data) {

					data = data.object
					var tempBox = '';
					for (let i = 0; i < data.length; i++) {
						tempBox += '<input class="selectBox" name="query" type="checkbox" value="' + data[
							i].id + '" /><span>' + data[i].name + '</span></br>'
					}
					$("#checkBoxes").append(tempBox)

				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					networkError(XMLHttpRequest, textStatus, errorThrown)
				}
			});


			// 获取当前的维修人员，设置为选中状态
			$.ajax({
				type: "get",
				url: URL + 'slaughterHouse/findRepairs?houseId=' + code,
				async: false,
				dataType: "json",
				success: function (data) {

					data = data.object
					var selectArr = $(".selectBox")

					// 存放所有的维修人员id
					var idArr = []

					for (let i = 0; i < selectArr.length; i++) {
						idArr.push($(selectArr[i]).attr("value"))
					}

					// 将当前屠宰场维修人员名字与所有维修人员名字进行比较
					for (let i = 0; i < data.length; i++) {

						for (let j = 0; j < idArr.length; j++) {
							// 如果名字相同则设为选中状态
							if (data[i].id === idArr[j]) {
								$(selectArr[j]).attr("checked", "checked")
							}
						}

					}

				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					networkError(XMLHttpRequest, textStatus, errorThrown)
				}
			});


			//表单验证
			$("form").validate({
				onkeyup: false,
				focusCleanup: true,
				success: "valid",
				submitHandler: function (form) {
					var index = parent.layer.getFrameIndex(window.name);

					var checked = $("#checkBoxes input[type='checkbox']:checked").val([]);
					var ids = [];
					for (let i = 0; i < checked.length; i++) {
						ids.push(checked[i].value)
					}

					// 将houseId从字符串类型转换为整数型
					var houseId = code - 0

					$(form).ajaxSubmit({
						type: "POST",
						url: URL + 'slaughterHouse/updateRepair',
						data: {
							"houseId": houseId,
							"ids": ids.join(",")
						},
						success: function (data) {
							parent.layer.msg('修改成功!', {
								icon: 1,
								time: 1000
							});
							setTimeout(function () {
								parent.$(".btn-refresh").click();
								parent.layer.close(index);
							}, 1500);
						},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							networkError(XMLHttpRequest, textStatus, errorThrown)
						}
					});

					// return false; // 阻止表单自动提交事件
				}
			});
		
		});
	</script>

</body>

</html>